package com.oyjd.ybx.business.classify.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.oyjd.ybx.bean.po.Classify;
import com.oyjd.ybx.bean.vo.ClassifyVO;
import com.oyjd.ybx.fw.action.Page;
import com.oyjd.ybx.fw.dao.BaseDao;

/**
 * 
 * 商品分类数据处理层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
public class ClassifyDao extends BaseDao {
	/**
	 * 获取商品分类列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Page<ClassifyVO> getList(Page<ClassifyVO> page) {
		String hqlResult = "select new " + ClassifyVO.class.getName() + ClassifyVO.STRUCT;
		String hqlCount = "select count(classify)";
		StringBuffer sb = new StringBuffer();
		sb.append(" from ").append(Classify.class.getName()).append(" classify");
		sb.append(" where 1=1");
		Map<String, String[]> paras = page.getParas();
		List<Object> ps = new ArrayList<Object>();
		for (String key : paras.keySet()) {
			if ("className".equals(key) || "classNote".equals(key)) {
				sb.append(" and classify.").append(key).append(" like ?");
				ps.add("%" + paras.get(key)[0] + "%");
			}
		}
		sb.append(" order by classify.orderKey");
		// do count
		Query qCount = getSession().createQuery(hqlCount + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			qCount.setParameter(i, ps.get(i));
		}
		Long count = (Long) qCount.uniqueResult();
		page.setCount(count);
		// do result
		Query q = getSession().createQuery(hqlResult + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			q.setParameter(i, ps.get(i));
		}
		q.setFirstResult(page.getFirst());
		q.setMaxResults(page.getPageSize());
		page.setResult(q.list());
		return page;
	}

	/**
	 * 根据ID获取商品分类对象信息
	 * 
	 * @param id
	 *            ID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public ClassifyVO getClassifyVO(String classId) {
		ClassifyVO result = null;
		StringBuffer sb = new StringBuffer();
		sb.append("select new ").append(ClassifyVO.class.getName()).append(ClassifyVO.STRUCT);
		sb.append(" from ").append(Classify.class.getName()).append(" classify");
		sb.append(" where classify.classId=?");
		Query q = this.getSession().createQuery(sb.toString());
		q.setParameter(0, classId);
		List<ClassifyVO> tmpList = (List<ClassifyVO>) q.list();
		if (tmpList.size() > 0) {
			result = tmpList.get(0);
		}
		return result;
	}

	/**
	 * 删除商品分类
	 * 
	 * @param id
	 *            多个ID
	 * @return
	 */
	public boolean remove(String id) {
		boolean flag = false;
		String hql = "select count(*) from " + Classify.class.getName() + " where pid=?";
		Query q = getSession().createQuery(hql);
		q.setParameter(0, id);
		Long count = (Long) q.uniqueResult();
		if (count == 0) {
			Query q1 = getSession().createQuery("delete from " + Classify.class.getName() + " classify where  classify.classId=?");
			q1.setParameter(0, id);
			q1.executeUpdate();
			flag = true;
		}
		return flag;
	}
	
	/**
	 * 获取大类下的二级菜单
	 * @param id 选择的大类
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<ClassifyVO> getLevel2(String id){
		StringBuffer sb = new StringBuffer();
		sb.append("select new ").append(ClassifyVO.class.getName()).append(ClassifyVO.STRUCT);
		sb.append(" from ").append(Classify.class.getName()).append(" classify");
		sb.append(" where classify.pid=? order by classify.orderKey");
		Query q = this.getSession().createQuery(sb.toString());
		q.setParameter(0, id);
		return (List<ClassifyVO>) q.list();		
	}
}
